package com.safeneeds.sysmana.ser.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.safeneeds.sysmana.entity.SysRole;
import com.safeneeds.sysmana.entity.SysRoleFunc;
import com.safeneeds.sysmana.entity.SysRoleFuncId;
import com.safeneeds.sysmana.entity.dao.ISysRoleDao;
import com.safeneeds.sysmana.entity.dao.ISysRoleFuncDao;
import com.safeneeds.sysmana.ser.ISysRoleService;
import com.safeneeds.util.base.RetType;
import com.safeneeds.util.other.StringUtil;
import com.safeneeds.util.page.PageInfo;
import com.safeneeds.util.page.ser.IPageService;

/**
 * 角色服务类
 * @author gaof
 * @date 2016年4月26日
 */
@Service
@Transactional
public class SysRoleService implements ISysRoleService {

	/* 岗位编号前缀 */
	private String roleNoPre = "R";
	
	@Resource(name="sysRoleDao")
	private ISysRoleDao sysRoleDao;
	
	@Resource(name="pageService")
	private IPageService pageService;
	
	@Resource(name="sysRoleFuncDao")
	private ISysRoleFuncDao sysRoleFuncDao;
	
	/* 功能、岗位数据缓存 */
	@Resource(name="funcRoleCache")
	private FuncRoleCacheService funcRoleCache;
	
	
	
	/**
	 * 添加或更新系统角色
	 * @param role
	 * @return
	 */
	@Override
	public RetType addOrUpdateRole(SysRole role) {
		
		//编辑岗位
		if(role.getRoleId() != null && role.getRoleId() > 0){
			updateRole(role);
		}else{//新增岗位
			addRole(role);
		}
		
		RetType reType = new RetType();
		reType.setErrCode(RetType.RET_SUCCESS);
		
		return reType;
	}

	/**
	 * 更新岗位
	 * @param role
	 */
	private void updateRole(SysRole role) {
		SysRole oldRole = sysRoleDao.getRoleById(role.getRoleId());
		if(oldRole == null){
			return;
		}
		
		oldRole.setRoleDesc(role.getRoleDesc());
		oldRole.setRoleName(role.getRoleName());
		oldRole.setLevel(role.getLevel());
		
		sysRoleDao.saveOrUpdateRole(oldRole);
	}

	/**
	 * 添加岗位
	 * @param role
	 */
	private void addRole(SysRole role) {
		
		String roleNo;
		Object maxRoleObject = sysRoleDao.getMaxRoleNoByPre(roleNoPre);
		if (null == maxRoleObject) {
			roleNo = roleNoPre + "001";
		} else {
			int roleNoInt = StringUtil.StringToInt(StringUtil.objToString(maxRoleObject).substring(1), 0);
			roleNoInt += 1;
			roleNo = roleNoPre
					+ StringUtil.rPad(String.valueOf(roleNoInt), '0', 3);
		}
		role.setRoleNo(roleNo);
		role.setStatus(ISysRoleDao.ROLE_STAT_OK);
		
		sysRoleDao.saveOrUpdateRole(role);
	}

	/**
	 * 获取系统岗位分页数据
	 * @param pageInfo
	 * @return
	 */
	@Override
	public PageInfo listRolePage(PageInfo pageInfo) {
		
		pageInfo = sysRoleDao.listRolePage(pageInfo);
		pageInfo = pageService.getPageInfoList(pageInfo);
		
		return pageInfo;
	}

	/**
	 * 通过角色id获取角色信息
	 * @param id
	 * @return
	 */
	@Override
	public SysRole getRoleById(Integer id) {
		return sysRoleDao.getRoleById(id);
	}

	/**
	 * 删除岗位
	 * @param ids
	 * @return
	 */
	@Override
	public RetType delRole(String ids) {
		
		String[] idSplit = ids.split(",");
		
		for (int i = 0; i < idSplit.length; i++) {
			int mid = StringUtil.StringToInt(idSplit[i], 0);
			
			SysRole role = sysRoleDao.getRoleById(mid);
			if(role == null){
				continue;
			}
			
			sysRoleDao.delRole(role);
			
			//删除岗位下所有权限
			sysRoleFuncDao.delRoleFuncByRoleId(role.getRoleId());
		}
		
		//更新岗位权限缓存
		funcRoleCache.reloadRoleFunc();
		
		RetType reType = new RetType();
		reType.setErrCode(RetType.RET_SUCCESS);
		
		return reType;
	}

	/**
	 * 岗位授权
	 * @param ids
	 * @param roleId
	 * @return
	 */
	@Override
	public RetType editRoleFunc(String ids, int roleId) {
		
		RetType retType = new RetType();
		
		if(roleId <= 0){
			retType.setErrCode(RetType.RET_FAILE);
			retType.setErrMsg("未传输岗位信息");
			
			return retType;
		}
		
		SysRole role = sysRoleDao.getRoleById(roleId);
		if(role == null){
			retType.setErrCode(RetType.RET_FAILE);
			retType.setErrMsg("岗位不存在");
			
			return retType;
		}
		
		//删除岗位下所有权限
		sysRoleFuncDao.delRoleFuncByRoleId(roleId);
		
		//添加本次授权
		List<SysRoleFunc> roleFuncList = new ArrayList<SysRoleFunc>();
		String[] idSplit = ids.split(",");
		for (int i = 0; i < idSplit.length; i++) {
			int mid = StringUtil.StringToInt(idSplit[i], 0);
			
			if(mid <= 0){
				continue;
			}
			
			SysRoleFunc roleFunc = new SysRoleFunc();
			SysRoleFuncId roleFuncId = new SysRoleFuncId();
			roleFuncId.setRoleId(roleId);
			roleFuncId.setFuncId(mid);
			roleFunc.setId(roleFuncId);
			roleFunc.setUpdateTime(new Date());
			roleFuncList.add(roleFunc);
		}
		sysRoleFuncDao.saveRoleFuncs(roleFuncList);
		
		//更新岗位权限缓存
		funcRoleCache.reloadRoleFunc();
		
		retType.setErrCode(RetType.RET_SUCCESS);
		
		return retType;
	}
	
	/**
	 * 获取系统角色（除去超级管理员）
	 * @return
	 */
	public List<SysRole> listAllRole(){
		return sysRoleDao.loadAll();
	}

}
